package com.michael.self.demompchart

import android.annotation.SuppressLint
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.MotionEvent
import com.github.mikephil.charting.charts.LineChart
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
import com.github.mikephil.charting.data.LineDataSet
import com.github.mikephil.charting.highlight.Highlight
import com.github.mikephil.charting.listener.ChartTouchListener
import com.github.mikephil.charting.listener.OnChartGestureListener
import com.github.mikephil.charting.listener.OnChartValueSelectedListener
import com.github.mikephil.charting.utils.MPPointF

class LineChartActivity : AppCompatActivity() {
    private var list= mutableListOf<Entry>()
    private var list2= mutableListOf<Entry>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_line_chart)
        val mLineChart = findViewById<LineChart>(R.id.lineChart)
        initChart(mLineChart)
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    private fun initChart(mLineChart: LineChart) {
        mLineChart.setTouchEnabled(true)
        mLineChart.setScaleEnabled(false)
//        mLineChart.xAxis.granularity = 500f
        mLineChart.xAxis.position = XAxis.XAxisPosition.BOTTOM
        //格式转化如果重置数据会超出size 异常
//        mLineChart.xAxis.valueFormatter =
//            IAxisValueFormatter { value, axis ->
//                Log.d("TAG", "getFormattedValue:${value} ")
//                value.toString()
//            }
        mLineChart.xAxis.axisMinimum=0f

        val mMyMarkerView=MyMarkerView(this,R.layout.layout_markview)
        mLineChart.marker=mMyMarkerView
        mLineChart.onChartGestureListener=object : OnChartGestureListener {
            override fun onChartGestureStart(
                me: MotionEvent?,
                lastPerformedGesture: ChartTouchListener.ChartGesture?
            ) {

            }

            override fun onChartGestureEnd(
                me: MotionEvent?,
                lastPerformedGesture: ChartTouchListener.ChartGesture?
            ) {
                //在这边做向右加载

            }

            override fun onChartLongPressed(me: MotionEvent?) {

            }

            override fun onChartDoubleTapped(me: MotionEvent?) {

            }

            override fun onChartSingleTapped(me: MotionEvent?) {

            }

            override fun onChartFling(
                me1: MotionEvent?,
                me2: MotionEvent?,
                velocityX: Float,
                velocityY: Float
            ) {

            }

            override fun onChartScale(me: MotionEvent?, scaleX: Float, scaleY: Float) {

            }

            override fun onChartTranslate(me: MotionEvent?, dX: Float, dY: Float) {


            }

        }
        mLineChart.setOnChartValueSelectedListener(object : OnChartValueSelectedListener {
            override fun onValueSelected(e: Entry?, h: Highlight?) {
                if (e==null||h==null) return
                list.onEach {
                    it.icon=null
                }
                list2.onEach {
                    it.icon=null
                }
                list[e.x.toInt()].icon=getDrawable(R.drawable.ic_oval)
                list2[e.x.toInt()].icon=getDrawable(R.drawable.ic_oval)
                updateChart(mLineChart)






                mMyMarkerView.offset= MPPointF().apply {
                    //判断线到上下左右的距离是否容得下markview
                    if (mLineChart.width-h.xPx<mMyMarkerView.width){
                        //左移
                        x=- (mMyMarkerView.width / 2).toFloat()*2
                    }else if (h.xPx-mLineChart.width<mMyMarkerView.width){
                        //右移
                        x=0f
                    }
                }

            }

            override fun onNothingSelected() {

            }

        })

        for (i in 0..10) {
            val mEntry=  Entry(i.toFloat(), (i + 20).toFloat())
            list.add(mEntry)
        }
        for (i in 0..10) {
            val mEntry=  Entry(i.toFloat(), (i + 5).toFloat())
            list2.add(mEntry)
        }
        updateChart(mLineChart)

    }

    private fun updateChart(mLineChart: LineChart){
        val dataSet = LineDataSet(list, "")
        val dataSet2 = LineDataSet(list2, "")
        //mpchart 里面的linechart如何设置点击后的颜色
        dataSet.highLightColor = Color.rgb(244, 117, 117)

        //设置点击后虚线
        dataSet. enableDashedHighlightLine(10f, 5f, 0f)
        dataSet.lineWidth=1.0f
        dataSet.setDrawCircleHole(false)
        dataSet.setDrawCircles(false)

        dataSet.mode= LineDataSet.Mode.LINEAR

        dataSet2.highLightColor = Color.rgb(244, 117, 117)

        //设置点击后虚线
        dataSet2. enableDashedHighlightLine(10f, 5f, 0f)
        dataSet2.lineWidth=1.0f
        dataSet2.setDrawCircleHole(false)
        dataSet2.setDrawCircles(false)

        dataSet2.mode= LineDataSet.Mode.LINEAR

        val data = LineData(dataSet,dataSet2)

        mLineChart.data = data
    }
}